package com.fingard.dsp.bank.directbank.cmbc01;

import com.fingard.dsp.bank.directbank.bankInterface.IPrivateSaleMsg;
import com.fingard.dsp.bank.directbank.ceb02.CEB02Base;
import com.fingard.dsp.bank.directbank.ceb02.CEB02Header;
import com.fingard.dsp.bank.directbank.cmbc02.CMBC02Base;
import com.fingard.dsp.bank.reqrespobj.privatesale.ReqPrivateSaleMsgQry;
import com.fingard.dsp.bank.reqrespobj.privatesale.RetAtsPrivateSaleDetail;
import com.fingard.dsp.bank.reqrespobj.privatesale.RetAtsPrivateSaleMsg;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

@Component("CMBC01PrivateSaleMsg")
public class CMBC01PrivateSaleMsg extends CMBC01Base implements IPrivateSaleMsg{

	@Override
	public RetAtsPrivateSaleMsg getPrivateSaleMsg(ReqPrivateSaleMsgQry reqInfo) throws Exception {
		RetAtsPrivateSaleMsg retAts = new RetAtsPrivateSaleMsg(reqInfo.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
        String tmpRespInfo = "";
		int PAGE = 0;
		int BISHU = 50;
        try {
        	for(int i=0;i<=PAGE;i++) {
            CMBC01Header bankHeader = new CMBC01Header();
            bankHeader.trnCode = "XR210012";
            XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
            writeCommonHeader(xmlWriter, bankHeader);
            //写入body
            xmlWriter.writeStartElement("xDataBody");
            xmlWriter.writeElementString("dealerCode",reqInfo.DealerCode);//经销商编号
            xmlWriter.writeElementString("stockDateStart", reqInfo.DiscoverySaleDate);//私售日期
            xmlWriter.writeElementString("stockDateEnd","");//截止反馈日期
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                    String ans_code = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                    String ans_info = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                        if ("0".equals(ans_code)) {
                        	tmpRespCode = "0";
                        	tmpRespInfo = "获取成功";
                        	String  DEALER_CODE = tmpBkReader.getFirstText("/CMBC/xDataBody/dealerCode");
                        	String  DEALER_NAME = tmpBkReader.getFirstText("/CMBC/xDataBody/dealerName");
                            while (tmpBkReader.read("/CMBC/xDataBody/Detail")) {
                            	RetAtsPrivateSaleDetail tmpRetDetail = new RetAtsPrivateSaleDetail();
                                tmpRetDetail.DEALER_CODE = DEALER_CODE;
                                tmpRetDetail.DEALER_NAME = DEALER_NAME;
                                String date = tmpBkReader.readedXElement.getSingleText("stockDate");
                                if(date!=null && date.contains("-")){
                                    date = date.replaceAll("-","");
                                }
                                tmpRetDetail.DISCOVERY_SALE_DATE = date;
                                tmpRetDetail.VIN = tmpBkReader.readedXElement.getSingleText("vin");
                                tmpRetDetail.kchgzh = tmpBkReader.readedXElement.getSingleText("kchgzh");
                                tmpRetDetail.kcfdjh = tmpBkReader.readedXElement.getSingleText("kcfdjh");
                                tmpRetDetail.warehouseName = tmpBkReader.readedXElement.getSingleText("warehouseName");
                                tmpRetDetail.moniStatus = tmpBkReader.readedXElement.getSingleText("moniStatus");//01：私移；02：私售
                                tmpRetDetail.RESERVE4 = tmpBkReader.readedXElement.getSingleText("RESERVE4");
                                tmpRetDetail.RESERVE5 = tmpBkReader.readedXElement.getSingleText("RESERVE5");
                                tmpRetDetail.RESERVE6 = tmpBkReader.readedXElement.getSingleText("RESERVE6");
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else {
                        	tmpRespCode = "-1";
                            tmpRespInfo = ans_info;
                            break ;
                        }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                    break ;
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
                break ;
            }
          }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }

}
